package com.miyuan.wm.exception;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.miyuan.wm.enums.ExceptionCodeEnum;
import com.miyuan.wm.response.BaseResponse;
import com.miyuan.wm.utils.MiscUtils;

/**
 * 全局异常处理器
 *
 *
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

	/**
	 * 输入参数校验异常
	 * 
	 * @param e
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@ExceptionHandler(value = MethodArgumentNotValidException.class)
	public BaseResponse handleException(MethodArgumentNotValidException e) {
		BindingResult bindingResult = e.getBindingResult();
		BaseResponse res = MiscUtils.getValidateError(bindingResult);
		return res;
	}

	/**
	 * 自定义异常处理
	 * 
	 * @param e
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@ExceptionHandler({ BaseException.class })
	public BaseResponse BaseExceptionHandler(ExceptionCodeEnum e) {
		return new BaseResponse(e);
	}
}
